            
class RoleinfoController 
{
	def jdbcTmplt;
    def userRoleDaoImpl
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save:'POST', update:'POST']

    def list = 
    {
        if(!params.max) params.max = 15;
        if(!params.offset) params.offset = 0;
        def nametj = "";
        def roleinfoInstanceList = [];
        def totalRow;   
        if(params.nametj)
        	nametj = params.nametj;
        if(nametj && nametj != "")
        {
        	if(params.sort && params.order)
        	{
        		roleinfoInstanceList = Roleinfo.findAllByRolenameLike("%${nametj}%", [max:params.max, offset:params.offset, sort:params.sort, order:params.order]);
        	}else
        	{
        		roleinfoInstanceList = Roleinfo.findAllByRolenameLike("%${nametj}%", [max:params.max, offset:params.offset]);
        	}
        	totalRow = Roleinfo.countByRolenameLike("%${nametj}%");       	
        }else
        {
        	if(params.sort && params.order)
        	{
        		roleinfoInstanceList = Roleinfo.list([max:params.max, offset:params.offset, sort:params.sort, order:params.order]);
        	}else
        	{
        		roleinfoInstanceList = Roleinfo.list([max:params.max, offset:params.offset]);
        	}
        	totalRow  = Roleinfo.count();
        }
        
        return [ roleinfoInstanceList:roleinfoInstanceList, totalRow:totalRow, nametj:nametj, max:params.max, offset:params.offset, sort:params.sort, order:params.order ]
    }

    
    def createRole=
	{ 
		def mList = userRoleDaoImpl.getAllMenus();
		return [m1List:mList[0], m2Map:mList[1], m3Map:mList[2]]
	}
	
	def userRoleSave=
	{
		def roleinfo = new Roleinfo(params);
		//roleinfo.name = params.rolename;
		if(!roleinfo.hasErrors() && roleinfo.save())
		{
			flash.message = "角色创建成功";
			redirect(action:list);
		}else
		{
			def mList = userRoleDaoImpl.getAllMenus();
			render(view:'createRole', model:[m1List:mList[0], m2Map:mList[1], m3Map:mList[2], roleinfo:roleinfo]);
		}
	}

    def show = 
    {
        def roleinfo = Roleinfo.get( params.id )
		def mList = userRoleDaoImpl.getUserRole(roleinfo.menus);
		return [rolename:roleinfo.rolename, rolememo:roleinfo.memo, m1List:mList[0], m2Map:mList[1], m3Map:mList[2]]
    }

    def edit = 
    {
        def roleinfo = Roleinfo.get( params.id )
		//def mList = userRoleDaoImpl.getUserRole(roleinfo.menus);
		def mList = userRoleDaoImpl.getAllMenus();
		return [roleinfo:roleinfo, m1List:mList[0], m2Map:mList[1], m3Map:mList[2]]
    }

    def roleUpdate = 
    {
        def roleinfo = Roleinfo.get( params.id.toLong() )
        roleinfo.rolename = params.rolename;
        roleinfo.memo = params.memo;
        roleinfo.menus = params.menus
        if(!roleinfo.hasErrors() && roleinfo.save()) 
        {
            flash.message = "角色修改成功"
            redirect(action:list)
        }else 
        {
            redirect(action:edit, id:params.id)
        }

    }
    
    def delete = 
    {
        def roleinfo = Roleinfo.get( params.id )
        if(roleinfo) 
        {
            roleinfo.delete()
            flash.message = "角色成功删除"
            redirect(action:list)
        }else 
        {
            flash.message = "没有发现该角色"
            redirect(action:list)
        }
    }

    def delBatch = 
    {
    	def batchIds = params.batchIds;
    	jdbcTmplt.update("delete from Roleinfo where id in (${batchIds})");
    	redirect(action:list);
    }
	
}